package com.slm.report.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.database.transaction.QueryTransaction;
import com.slm.mylibrary.base.BaseFragment;
import com.slm.report.databinding.ActivityCalendarBinding;
import com.slm.report.db.DuShuInfoBean_Table;
import com.slm.report.db.SignInInfoBean;
import com.slm.report.db.SignInInfoBean_Table;
import com.slm.report.db.StudentInfoBean;
import com.slm.report.state.StudentInfoViewModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: My Application
 * @Package: com.slm.report.ui.fragment
 * @ClassName: CalendarFragment
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2020/5/25 16:49
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/5/25 16:49
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class CalendarFragment extends BaseFragment<StudentInfoViewModel> implements CalendarView.OnCalendarSelectListener,
        CalendarView.OnYearChangeListener {

    private ActivityCalendarBinding viewBinding;
    private int mYear;
    private StudentInfoBean studentInfoBean;
    private List<SignInInfoBean> signInInfoBeans = new ArrayList<>();
    private Map<String, Calendar> calendarHashMap = new HashMap<>();

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewBinding = ActivityCalendarBinding.inflate(LayoutInflater.from(getContext()));
        mViewModel.studentInfoBean.observe(this, new Observer<StudentInfoBean>() {
            @Override
            public void onChanged(StudentInfoBean infoBean) {
                studentInfoBean = infoBean;
                obtainData();
            }
        });

        viewBinding.addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SignInInfoBean inInfoBean = SQLite.select()
                        .from(SignInInfoBean.class)
                        .where(SignInInfoBean_Table.studentId.eq(studentInfoBean.getId()))
                        .and(SignInInfoBean_Table.time.eq(TimeUtils.getNowString(TimeUtils.getSafeDateFormat("yyyy-M-d"))))
                        .querySingle();

                if (ObjectUtils.isEmpty(inInfoBean)) {
                    SignInInfoBean signInInfoBean = new SignInInfoBean();
                    signInInfoBean.setStudentId(studentInfoBean.getId());
                    signInInfoBean.setCreateTime(System.currentTimeMillis());
                    signInInfoBean.setTime(TimeUtils.getNowString(TimeUtils.getSafeDateFormat("yyyy-M-d")));
                    signInInfoBean.save();
                    String[] datas = signInInfoBean.getTime().split("-");
                    Calendar calendar = new Calendar();
                    calendar.setYear(Integer.parseInt(datas[0]));
                    calendar.setMonth(Integer.parseInt(datas[1]));
                    calendar.setDay(Integer.parseInt(datas[2]));
                    calendar.setScheme("打");
                    calendar.setSchemeColor(0xFF13acf0);
                    calendarHashMap.put(calendar.toString(), calendar);
                    viewBinding.calendarView.setSchemeDate(calendarHashMap);
                    ToastUtils.showShort("添加成功");
                } else {
                    ToastUtils.showShort("今天已经打过卡");

                }

            }
        });

        viewBinding.tvMonthDay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!viewBinding.calendarLayout.isExpand()) {
                    viewBinding.calendarLayout.expand();
                    return;
                }
                viewBinding.calendarView.showYearSelectLayout(mYear);
                viewBinding.tvLunar.setVisibility(View.GONE);
                viewBinding.tvYear.setVisibility(View.GONE);
                viewBinding.tvMonthDay.setText(String.valueOf(mYear));
            }
        });

        viewBinding.flCurrent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewBinding.calendarView.scrollToCurrent();
            }
        });
        viewBinding.calendarView.setOnCalendarSelectListener(this);
        viewBinding.calendarView.setOnYearChangeListener(this);
        viewBinding.tvYear.setText(String.valueOf(viewBinding.calendarView.getCurYear()));
        mYear = viewBinding.calendarView.getCurYear();
        viewBinding.tvMonthDay.setText(viewBinding.calendarView.getCurMonth() + "月" + viewBinding.calendarView.getCurDay() + "日");
        viewBinding.tvLunar.setText("今日");
        viewBinding.tvCurrentDay.setText(String.valueOf(viewBinding.calendarView.getCurDay()));
        return viewBinding.getRoot();
    }


    private void obtainData() {
        SQLite.select()
                .from(SignInInfoBean.class)
                .where(SignInInfoBean_Table.studentId.eq(studentInfoBean.getId()))
                .orderBy(DuShuInfoBean_Table.createTime, false)
                .async()
                .queryListResultCallback(new QueryTransaction.QueryResultListCallback<SignInInfoBean>() {
                    @Override
                    public void onListQueryResult(QueryTransaction transaction, @NonNull List<SignInInfoBean> tResult) {
                        signInInfoBeans.clear();
                        signInInfoBeans.addAll(tResult);
                        calendarHashMap.clear();
                        for (SignInInfoBean signInInfoBean : signInInfoBeans) {
                            String[] datas = signInInfoBean.getTime().split("-");
                            Calendar calendar = new Calendar();
                            calendar.setYear(Integer.parseInt(datas[0]));
                            calendar.setMonth(Integer.parseInt(datas[1]));
                            calendar.setDay(Integer.parseInt(datas[2]));
                            calendar.setScheme("打");
                            calendar.setSchemeColor(0xFF13acf0);
                            calendarHashMap.put(calendar.toString(), calendar);
                        }
                        viewBinding.calendarView.setSchemeDate(calendarHashMap);
                    }
                })
                .execute();

    }


    @Override
    public void onCalendarOutOfRange(Calendar calendar) {

    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {
        viewBinding.tvLunar.setVisibility(View.VISIBLE);
        viewBinding.tvYear.setVisibility(View.VISIBLE);
        viewBinding.tvMonthDay.setText(calendar.getMonth() + "月" + calendar.getDay() + "日");
        viewBinding.tvYear.setText(String.valueOf(calendar.getYear()));
        viewBinding.tvLunar.setText(calendar.getLunar());
        mYear = calendar.getYear();
    }


    @Override
    public void onYearChange(int year) {
        viewBinding.tvMonthDay.setText(String.valueOf(year));
    }
}
